package com.lancoo.answer.helper;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;

import com.lancoo.answer.R;

/**
 * Created by pengmi on 2022/1/16 15:43
 * Description:通用
 */
public class SingleConfirmDialogHelper {

    private static long subTime;

    /**
     * 确认弹窗
     * @param mContext context
     * @param callBack callback
     * @return AlertDialog
     */

    public static AlertDialog showConfirmDialog(Context mContext,String title,
                                                String content,String sureStr,final ConfirCallBack callBack){
        View inflate= LayoutInflater.from(mContext).inflate(R.layout.ev_dialog_single_confirm,null);
        AlertDialog.Builder builder=new AlertDialog.Builder(mContext,R.style.ev_dialogStyle);
        builder.setView(inflate);
        builder.setCancelable(false);
        final AlertDialog alertDialog = builder.create();
        alertDialog.show();
        Window window = alertDialog.getWindow();
        if(window==null){
            return alertDialog;
        }
        WindowManager.LayoutParams attributes = window.getAttributes();
        int width =mContext.getResources().getDisplayMetrics().widthPixels;
        int px280 = (int)mContext.getResources().getDisplayMetrics().density*280;
        attributes.width=Math.min(width*280/375,px280);
        attributes.dimAmount=0.3f;
        alertDialog.getWindow().setAttributes(attributes);
        TextView tv_sure = inflate.findViewById(R.id.tv_sure);
        TextView tv_content = inflate.findViewById(R.id.tv_content);
        TextView tv_title = inflate.findViewById(R.id.tv_title);
        tv_sure.setText(sureStr);
        tv_content.setText(content);
        tv_title.setText(title);
        inflate.findViewById(R.id.tv_sure).setOnClickListener(v -> {
            if(System.currentTimeMillis()-subTime<200){
                return;
            }
            subTime=System.currentTimeMillis();
            alertDialog.dismiss();
            callBack.onSure();
        });
        return alertDialog;
    }


    public static AlertDialog showConfirmDialog(Context mContext,
                                                String content,String sureStr ,final ConfirCallBack callBack){
        return showConfirmDialog(mContext,"温馨提示",content,sureStr,callBack);
    }

    public abstract static class ConfirCallBack{

        public void onCancel(){}

        public void onSure(){}

    }

}
